package cc.linker.steplink.crm.controller;

import cc.linker.steplink.crm.merchant.task.AcsAccountSummaryStatisticsTask;
import cc.linker.steplink.crm.merchanth5.service.MaiBaoLaService;
import cc.linker.steplink.crm.response.GlobalException;
import cc.linker.steplink.crm.task.*;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

/**
 * 直接调用长周期定时任务，仅测试使用
 */
@RestController
@RequestMapping("/task")
public class TaskController {

    @Autowired
    private ArticleLabelService articleLabelService;
    @Autowired
    private GroupStatistics groupStatistics;
    @Autowired
    private AutoMarkLabelTask autoMarkLabelTask;
    @Autowired
    private SyncPropagationLinkTask syncPropagationLinkTask;
    @Autowired
    private AnalysePropagationLinkTask analysePropagationLinkTask;
    @Autowired
    private ScoreActivityTask scoreActivityTask;
    @Autowired
    private RobotUserPortraitTask robotUserPortraitTask;
    @Autowired
    private AcsAccountSummaryStatisticsTask acsAccountSummaryStatisticsTask;
    @Autowired
    private PrecisionPush precisionPush;
    @Autowired
    private SyncPublicUserDataTask syncPublicUserDataTask;
    @Autowired
    private MaiBaoLaService maiBaoLaService;

    @GetMapping("/syncPublicUserDataTask")
    public void syncPublicUserDataTask(String orgId) {
        throw GlobalException.build("暂不开放");
//        syncPublicUserDataTask.doSync(orgId);
    }

    @GetMapping("/precisionPush")
    public void precisionPush() {
        precisionPush.doPush();
    }

    @GetMapping("/accountSummary")
    public void accountSummary(String day) {
        acsAccountSummaryStatisticsTask.accountSummary(day);
    }

    @GetMapping("/accountSummaryMonth")
    public void accountSummaryMonth(int year,int month) {
        List<String> dayList = getDayByMonth(year,month);
        for(String day : dayList){
            acsAccountSummaryStatisticsTask.accountSummary(day);
        }

    }

    @GetMapping("/accountSummarySalerMonth")
    public void accountSummarySalerMonth(int year,int month) {
        List<String> dayList = getDayByMonth(year,month);
        for(String day : dayList){
            acsAccountSummaryStatisticsTask.accountSummarySaler(day);
        }
    }

    @GetMapping("/accountSummarySaler")
    public void accountSummarySaler(String day) {
        acsAccountSummaryStatisticsTask.accountSummarySaler(day);
    }

    @GetMapping("/ArticleLabelService")
    public void run1() {
        articleLabelService.service();
    }

    @GetMapping("/GroupStatistics")
    public void run3() {
        groupStatistics.doStatistics();
    }

    @GetMapping("/autoMarkLabelTask")
    public void autoMarkLabelTask() {
        autoMarkLabelTask.work();
    }

    @GetMapping("/syncPropagationLinkTask")
    public void syncPropagationLinkTask() {
        syncPropagationLinkTask.execute();
    }

    @GetMapping("/analysePropagationLinkTask")
    public void analysePropagationLinkTask(String day) {
        analysePropagationLinkTask.execute(day);
    }

    @GetMapping("/scoreActivityTask")
    public void scoreActivityTask(String day) {
        scoreActivityTask.execute(day);
    }

    /**
     * 选择某个机构下所有个人 开户操作
     */
    @GetMapping("/createMember")
    public void createMember(String orgId,String sectet){
        if(!"thisissecret".equals(sectet)){
            throw GlobalException.build("秘钥不正确哈");
        }
        maiBaoLaService.orgCreateMember(orgId);
    }

    /**
     * @Description: 供测试调用修改机器人
     * @Param:
     * @Return:
     * @Author: 锋
     * @Date: 2019/9/11 20:10
     * @Version:
     */
    @GetMapping("/callActiveTask")
    public String callActiveTask() {
        robotUserPortraitTask.userActiveTask();
        return "called";
    }

    /**
     * @Description: 供测试调用修改机器人
     * @Param:
     * @Return:
     * @Author: 锋
     * @Date: 2019/9/11 20:10
     * @Version:
     */
    @GetMapping("/callMarketRobot")
    public String callMarketRobot() {
        robotUserPortraitTask.marketRobotUserPortraitTask();
        return "called";
    }

    /**
     * @Description: 供测试调用修改机器人
     * @Param:
     * @Return:
     * @Author: 锋
     * @Date: 2019/9/11 20:10
     * @Version:
     */
    @GetMapping("/callClientRobot")
    public String callClientRobot() {
        robotUserPortraitTask.clientRobotUserPortraitTask();
        return "called";
    }

    /**
     * @Description: 用户活跃度调整
     * @Param: []
     * @Return: void
     * @Author: 锋
     * @Date: 2019/8/30 13:35
     * @Version:
     */
    @GetMapping("/userActiveTask")
    public String userActiveTask() {
        robotUserPortraitTask.userActiveTask();
        return "called";
    }


    public static List<String> getDayByMonth(int yearParam,int monthParam){
        List list = new ArrayList();
        Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
        aCalendar.set(yearParam,(monthParam-1),1);
        int year = aCalendar.get(Calendar.YEAR);//年份
        int month = aCalendar.get(Calendar.MONTH) + 1;//月份
        int day = aCalendar.getActualMaximum(Calendar.DATE);
        for (int i = 1; i <= day; i++) {
            String aDate=null;
            if(month<10&&i<10){
                aDate = String.valueOf(year)+"-0"+month+"-0"+i;
            }
            if(month<10&&i>=10){
                aDate = String.valueOf(year)+"-0"+month+"-"+i;
            }
            if(month>=10&&i<10){
                aDate = String.valueOf(year)+"-"+month+"-0"+i;
            }
            if(month>=10&&i>=10){
                aDate = String.valueOf(year)+"-"+month+"-"+i;
            }

            list.add(aDate);
        }
        return list;
    }
}

